﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using McReports.Common;
using McReports.ViewModel;

namespace MContracts.Controls.Dialogs
{
    /// <summary>
    /// Interaction logic for ReportParamsWindow.xaml
    /// </summary>
    public partial class ReportParamsWindow : IUiQueryParametersProvider
    {
        public ReportParamsWindow()
        {
            InitializeComponent();
        }
            
 

        private void BuildUI(BaseReportViewModel reportViewModel)
        {
            System.Windows.Data.Binding myBinding = new System.Windows.Data.Binding
            {
                StringFormat = "0:N"
            };



            foreach (var reportParameter in reportViewModel.Parameters)
            {
                var paramSp = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(10, 10, 10, 5) };

                var paramLbl = new Label
                {
                    VerticalAlignment = VerticalAlignment.Top,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Content = reportParameter.Attribute.DisplayName,
                    Margin = new Thickness(0, 0, 10, 0)
                };


                paramSp.Children.Add(paramLbl);


                if (reportParameter.Attribute.DesiredType.BaseType == typeof(Enum))
                {
                    var paramCntr = new ComboBox
                    {
                        VerticalAlignment = VerticalAlignment.Top,
                        MinWidth = 150,
                        Name = reportParameter.PropertyName
                    };


                    ObjectDataProvider objectDataProvider = new ObjectDataProvider
                    {
                        MethodName = "GetValues",
                        ObjectType = typeof(Enum)
                    };
                    objectDataProvider.MethodParameters.Add(reportParameter.Attribute.DesiredType);
                    objectDataProvider.Refresh();


                    paramCntr.ItemsSource = (objectDataProvider.Data as IEnumerable);

                    Binding binding = new Binding();
                    binding.Source = reportViewModel;
                    binding.Path = new PropertyPath(reportParameter.PropertyName);
                    binding.Mode = BindingMode.TwoWay;


                    paramCntr.SetBinding(ComboBox.SelectedItemProperty, binding);


                    paramSp.Children.Add(paramCntr);
                }

                else
                {
                    var paramTb = new TextBox { VerticalAlignment = VerticalAlignment.Top, MinWidth = 150 };

                    paramTb.Name = reportParameter.PropertyName;
                    Binding binding = new Binding();
                    binding.Source = reportViewModel;
                    binding.Path = new PropertyPath(reportParameter.PropertyName);
                    binding.Mode = BindingMode.TwoWay;
                    paramTb.SetBinding(TextBox.TextProperty, binding);



                    paramSp.Children.Add(paramTb);
                }

                SP.Children.Add(paramSp);
            }
        }

  
        private void Button_OK_Click(object sender, RoutedEventArgs e)
        {

            DialogResult = true;

            Close();
        }

        private void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        public BaseReportViewModel ViewModel { get; set; }
        

        public bool GetParameters()
        {
            return ShowDialog().GetValueOrDefault();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            BuildUI(ViewModel);
        }
    }
}
